//
//  UIImage+Crop.m
//  PrintFamily
//
//  Created by edaihua on 16/2/28.
//  Copyright © 2016年 ___PrintFactory___. All rights reserved.
//

#import "UIImage+Crop.h"

@implementation UIImage (Crop)


+ (UIImage *)fixrotation:(UIImage *)image {
    
    if (image.imageOrientation == UIImageOrientationUp) return image;
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (image.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, image.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }
    
    switch (image.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                             CGImageGetBitsPerComponent(image.CGImage), 0,
                                             CGImageGetColorSpace(image.CGImage),
                                             CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (image.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;    
}

+ (CGFloat)translateYFromOriginX:(CGFloat)originX currentY:(CGFloat)originY orientation:(UIImageOrientation)orientation {
    CGFloat resultY = originY;
    switch (orientation) {
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            break;
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            resultY = -originX;
            break;
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            resultY = originX;
            break;
    }
    return resultY;
}

+ (CGFloat)translateXFromOriginY:(CGFloat)originY currentX:(CGFloat)originX orientation:(UIImageOrientation)orientation {
    CGFloat resultX = originX;
    switch (orientation) {
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            break;
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            resultX = originY;
            break;
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            resultX = -originY;
            break;
    }
    return resultX;
}

+ (CGFloat)translateWidthFromHeight:(CGFloat)height currentWidth:(CGFloat)width orientation:(UIImageOrientation)orientation {
    CGFloat resultWidth = width;
    switch (orientation) {
        case UIImageOrientationUp:
        case UIImageOrientationDown:
            break;
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            resultWidth = height;
            break;
        default:
            break;
    }
    return resultWidth;
}

+ (CGFloat)translateHeightFromWidth:(CGFloat)width currentHeight:(CGFloat)height orientation:(UIImageOrientation)orientation {
    CGFloat resultHeight = height;
    switch (orientation) {
        case UIImageOrientationUp:
        case UIImageOrientationDown:
            break;
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            resultHeight = width;
            break;
        default:
            break;
    }
    return resultHeight;
}

+ (CGRect)translateRectFromRect:(CGRect)rect orientation:(UIImageOrientation)orientation {
    CGRect resultRect = CGRectZero;
    resultRect.origin.x = [UIImage translateXFromOriginY:rect.origin.y currentX:rect.origin.x orientation:orientation];
    resultRect.origin.y = [UIImage translateYFromOriginX:rect.origin.x currentY:rect.origin.y orientation:orientation];
    resultRect.size.width = [UIImage translateWidthFromHeight:rect.size.height currentWidth:rect.size.width orientation:orientation];
    resultRect.size.height = [UIImage translateHeightFromWidth:rect.size.width currentHeight:rect.size.height orientation:orientation];
    return resultRect;
}

static inline double radians (double degrees) {return degrees * M_PI/180;}

+ (UIImage*)rotate:(UIImage*)image orientation:(UIImageOrientation)orientation {
    UIGraphicsBeginImageContext(image.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    switch (orientation) {
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            CGContextRotateCTM (context, radians(90));
            break;
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            CGContextRotateCTM (context, -radians(90));
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            CGContextRotateCTM (context, radians(90));
            break;
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
        default:
            break;
    }
    [image drawAtPoint:CGPointMake(0, 0)];
    
    UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return rotatedImage;
}

- (UIImage*)cropImageWithRect:(CGRect)cropRect {
    
    CGRect translatedRect = [UIImage translateRectFromRect:cropRect orientation:self.imageOrientation];
    
    CGImageRef squreImageRef = CGImageCreateWithImageInRect(self.CGImage, translatedRect);
    UIImage *croppedImage = [UIImage imageWithCGImage:squreImageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(squreImageRef);
    return croppedImage;
}

- (UIImage*)getCroppedPotraitImageToFitSize:(CGSize)rectSize cropPoint:(CGPoint)originPoint orientation:(UIImageOrientation)orientation {
    CGRect translatedRect = CGRectZero;
    CGRect imageRect = CGRectZero;
    CGSize imageSize = self.size;
    
    CGFloat heightWidthRatio = rectSize.height / rectSize.width;
    CGFloat imageHeightWidthRation = imageSize.height / imageSize.width;
    
    if (imageHeightWidthRation < heightWidthRatio) {
        imageRect = CGRectMake((imageSize.width - (imageSize.height / heightWidthRatio)) / 2, 0, imageSize.height / heightWidthRatio, imageSize.height);
    } else if (imageHeightWidthRation > heightWidthRatio){
        imageRect = CGRectMake(0, (imageSize.height - imageSize.width * heightWidthRatio) / 2 , imageSize.width, imageSize.width * heightWidthRatio);
    }
    
    translatedRect = [UIImage translateRectFromRect:imageRect orientation:orientation];
    
    CGImageRef squreImageRef = CGImageCreateWithImageInRect(self.CGImage, translatedRect);
    UIImage *croppedImage = [UIImage imageWithCGImage:squreImageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(squreImageRef);
    return croppedImage;
}

- (UIImage*)getCroppedLandscapeImageToFitThePhotoSize:(CGSize)rectSize withOrientation:(UIImageOrientation)orientation {
    CGRect translatedRect = CGRectZero;
    CGRect imageRect = CGRectZero;
    CGSize imageSize = self.size;
    
    CGFloat heightToWidthRatio = rectSize.height / rectSize.width;
    if (imageSize.height / imageSize.width < heightToWidthRatio) {
        imageRect = CGRectMake((imageSize.width - (imageSize.height / heightToWidthRatio)) / 2, 0, imageSize.height / heightToWidthRatio, imageSize.height);
    } else if (imageSize.height / imageSize.width > heightToWidthRatio){
        imageRect = CGRectMake(0, (imageSize.height - imageSize.width * heightToWidthRatio) / 2 , imageSize.width, imageSize.width * heightToWidthRatio);
    }
    
    translatedRect = [UIImage translateRectFromRect:imageRect orientation:orientation];
    
    CGImageRef squreImageRef = CGImageCreateWithImageInRect(self.CGImage, translatedRect);
    UIImage *croppedImage = [UIImage imageWithCGImage:squreImageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(squreImageRef);
    return croppedImage;
}

- (UIImage*)getImageInCenterAtSize:(CGSize)rectSize {
    
    CGSize imageSize = [self size];
    if (imageSize.width < rectSize.width && imageSize.height < rectSize.height) {
        return self;
    }
    
    CGRect imageRect = CGRectZero;
    if (imageSize.width > imageSize.height) {
        float originX = (imageSize.width - imageSize.height)/2;
        imageRect = CGRectMake(originX, 0, imageSize.height, imageSize.height);
    } else {
        float originY = (imageSize.height - imageSize.width)/2;
        imageRect = CGRectMake(0, originY, imageSize.width, imageSize.width);
    }
    
    CGImageRef squreImageRef = CGImageCreateWithImageInRect(self.CGImage, imageRect);
    UIImage *centerImage = [UIImage imageWithCGImage:squreImageRef];
    CGImageRelease(squreImageRef);
    return centerImage;
}

@end
